package com.cloud.utils;

import com.google.common.collect.Maps;
import com.sun.management.OperatingSystemMXBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.util.Map;

@Component
@Slf4j
public class SystemInfoUtils {

    public Map getMem() {
        Map result = Maps.newHashMap();
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        // 堆内存使用情况
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        // 初始的总内存
        long initTotalMemorySize = memoryUsage.getInit();
        // 最大可用内存
        long maxMemorySize = memoryUsage.getMax();
        // 已使用的内存
        long usedMemorySize = memoryUsage.getUsed();
        Map sysUsageMap = Maps.newHashMap();

        // 总的物理内存
        String totalMemorySize = new DecimalFormat("#.##").format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
        // 剩余的物理内存
        String freePhysicalMemorySize = new DecimalFormat("#.##").format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
        // 已使用的物理内存
        String usedMemory = new DecimalFormat("#.##").format((osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024.0 / 1024 / 1024) + "G";
        sysUsageMap.put("总的物理内存", totalMemorySize);
        sysUsageMap.put("剩余的物理内存", freePhysicalMemorySize);
        sysUsageMap.put("已使用的物理内存", usedMemory);
        Map jvmUsageMap = Maps.newHashMap();
        String jvmInitMem = new DecimalFormat("#.#").format(initTotalMemorySize * 1.0 / 1024 / 1024) + "M";
        String jvmMaxMem = new DecimalFormat("#.#").format(maxMemorySize * 1.0 / 1024 / 1024) + "M";
        String jvmUsedMem = new DecimalFormat("#.#").format(usedMemorySize * 1.0 / 1024 / 1024) + "M";
        jvmUsageMap.put("JVM初始总内存", jvmInitMem);
        jvmUsageMap.put("JVM最大可用内存", jvmMaxMem);
        jvmUsageMap.put("JVM已使用的内存", jvmUsedMem);
        result.put("系统内存使用情况", sysUsageMap);
        result.put("jvm内存使用情况", jvmUsageMap);
        result.put("jvm内存使用占比", new DecimalFormat("#.##%").format(usedMemorySize * 1.0 / maxMemorySize));
        result.put("磁盘使用情况",this.getSpace());
        return result;
    }

    public Map getSpace() {
        Map result = Maps.newHashMap();
        // 磁盘使用情况
        File[] files = File.listRoots();
        for (File file : files) {
            String total = new DecimalFormat("#.#").format(file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
            String free = new DecimalFormat("#.#").format(file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
            String un = new DecimalFormat("#.#").format(file.getUsableSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
            String path = file.getPath();
            System.err.println(path + "总:" + total + ",可用空间:" + un + ",空闲空间:" + free);
            result.put(path + "总的磁盘空间", total);
            result.put(path + "可用磁盘空间", un);
            result.put(path + "空闲磁盘空间", free);
        }
        return result;
    }
}